<?php
 
class Blog extends MY_Controller 
{
	var $curl = 'blog';
	var $name = 'Blog Manager';
	var $search = '';
	var $language;
	var $submenu = "_menu";
	var $id_category;

	function __construct()
	{
		parent::__construct();
        
        $this->load->model(array('/site/blog_model', '/site/comment_model'));
        $this->load->model('/site/post_model');
        $this->load->model('/settings/settings_model');
       
		$this->validation->set_fields($this->post_model->fields);

		$this->data['Title'] = T_('Blog Manager');

		$this->language = parent::getLang();
		
		$this->name = T_('Blog Manager');		
    }
	
    function index() {
    	$this->renderLayout();
    }
	function posts()
	{		
		$this->post_model->search();
		$this->renderLayout();
    }

    function createPost(){
        
        if (count($_POST))
		{
			$rules = array('title', 'teaser', 'text', 'status');
			$this->validation->set_rules($this->post_model->getRules($rules));

			if ($this->validation->run() === TRUE)
			{
				$item['br_title'] 	= $this->validation->title;
				$item['br_teaser']      = $this->validation->teaser;
				$item['br_text'] 	= $this->validation->text;
				$item['br_status']      = $this->validation->status;
                $author                 = $this->settings_model->get('blog_author');
                $item['br_author']      = $author->value;

                if($item['br_teaser']==''){
                    $item['br_teaser'] = substr($item['br_text'],0,255).'...';
                }

                if ($this->post_model->insert($item))
				{
					redirect($this->curl . '/posts/');
				}
			}
		}

		$this->data['Title'] = T_('New post');
		$this->data['wysiwyg'] = true; 
		//$this->data['Title'] = $this->blog_model->category[ $this->id_category ];
		
		//$this->blog_model->search();
		//$this->search = $this->data['search'];
		
		$this->renderLayout();
    }
	function comments()
	{		
		$this->comment_model->search();
		
		$this->data['Title'] = $this->comment_model->title;
		//$this->data['Title'] = $this->post_model->category[ $this->id_category ];
		
		$this->renderLayout();
    }
    function createComment() {
        
    	if (count($_POST))
		{
			$rules = array('id', 'date_created', 'author', 'email', 'status', 'under_post', 'text');
			$this->validation->set_rules($this->comment_model->getRules($rules));

			if ($this->validation->run() === TRUE)
			{	
				//$item['bc_id'] 					= $this->validation->id;
				$item['bc_date_created'] 			= date('Y-m-d H:i:s');
				$item['bc_text'] 					= $this->validation->text;
				$item['bc_status'] 					= $this->validation->status;				
				$item['bc_author'] 					= $this->validation->author;
				$item['bc_email'] 					= $this->validation->email;
				$item['bc_under_post'] 				= $this->validation->under_post;										
				
				if ($this->comment_model->insert($item))
				{
					redirect($this->curl . '/comments/');	
				}				
			}
		}

		$this->data['Title'] = $this->comment_model->title .' / '. T_('Create');
		$this->data['wysiwyg'] = true;
		$this->post_model->search();
		//$this->search = $this->data['search'];
	
		$this->renderLayout();
    }
	function editComment($id = null) {
    	
    	$this->validation->set_fields($this->comment_model->fields);
    	$rules = array('id', 'date_created', 'author', 'email', 'status', 'under_post', 'text');
		$this->validation->set_rules($this->comment_model->getRules($rules));
		
		if (!empty($id))
		{
			$object = $this->comment_model->get($id);
			
			if (!count($object))
			{
				$this->data['view'] = '_elements/query_no_object';
				$this->load->view($this->data['layout'], $this->data);
				return false; 
			}
		
			if (!count($_POST))
			{
				$this->validation->id 				= $object['bc_id']; 
	        	$this->validation->date_created 	= $object['bc_date_created'];
	        	$this->validation->text 			= $object['bc_text']; 	        	
	        	$this->validation->under_post 		= $object['bc_under_post'];
	        	$this->validation->email 			= $object['bc_email'];
	        	$this->validation->author 			= $object['bc_author'];		
	        	$this->validation->status 			= $object['bc_status'];	
			}
			else
			{
				
				
				if ($this->validation->run() === TRUE)
				{
					$item['bc_id'] 					= $this->validation->id;
					$item['bc_date_created'] 		= $this->validation->date_created;
					$item['bc_text'] 				= $this->validation->text;
					$item['bc_status'] 				= $this->validation->status;				
					$item['bc_author'] 				= $this->validation->author;
					$item['bc_email'] 				= $this->validation->email;
					$item['bc_under_post'] 			= $this->validation->under_post;															
					
					$this->comment_model->update($item);					
					
					redirect($this->curl.'/comments/');				
				}
			}			
		}
           
		$this->data['Title'] = $this->comment_model->title .' / '. T_('Edit');		
		$this->data['wysiwyg'] = true; 
		
		$this->renderLayout();
    }
	
	function editPost($id = null)
	{
        if (!empty($id))
		{
			$object = $this->post_model->get($id);
            
	
			if (!count($_POST))
			{
				$this->validation->id 		= $object['br_id'];
				$this->validation->title 	= $object['br_title'];
				$this->validation->teaser 	= $object['br_teaser'];
	        	$this->validation->text 	= $object['br_text'];
	        	$this->validation->status 	= $object['br_status'];
                
			}else{
				$rules = array('title', 'teaser', 'text', 'status');
				$this->validation->set_rules($this->post_model->getRules($rules));
	
				if ($this->validation->run() === TRUE)
				{
					$item['br_id']          = $this->validation->id;
                    $item['br_title'] 		= $this->validation->title;
					$item['br_teaser']      = $this->validation->teaser;
					$item['br_text'] 		= $this->validation->text;
                    $item['br_status']      = $this->validation->status;
					$item['br_date_modified'] = date('Y-m-d H:i:s');

                    $this->post_model->update($item);
					
					redirect($this->curl . '/posts');
				}
			}			
		}/*else{
          		$this->data['view'] = '_elements/query_no_object';
				$this->load->view($this->data['layout'], $this->data);
				return false;
	    }*/
           
		$this->data['Title'] =  T_('Edit post');
		$this->data['wysiwyg'] = true; 
		
		$this->renderLayout();
    }
        
    function insert_category()
    {
        $page_name = trim($this->input->post('categoryname'));
        $parentid = (int)$this->input->post('categoryid');
        $parentNode = $this->cats->getNodeFromId($parentid);
        
        $fields_array = array("categoryname"=>$page_name);
        $this->cats->appendNewChild($parentNode,$fields_array);
        
        $this->_render_page();
    }
        
	function deletePost($id)
	{
		$data = $this->post_model->delete($id);

		if ($data)
		{
			redirect($this->curl . '/posts');
		}
		else
		{
			redirect($this->curl . '/editPost/'. $id);
		}
	}
	function deleteComments() {
		
		$data = $this->comment_model->delete($_REQUEST['items']);
		
		echo $data;	
	}
	function deleteComment($id) {
		if(isset($id)) {
			$data = $this->comment_model->delete($id);
			if ($data)
			{
				redirect($this->curl . '/comments/');	
			}
			else
			{
				redirect($this->curl . '/editComments/'. $id . '/');
			}
		} 	
	}            
	function deleteItems()
	{
		$data = $this->post_model->delete($_REQUEST['items']);

		echo $data;
	}
	    
	function activity()
	{
		$data = $this->utils->publishItems($this->article_model->table, $this->article_model->prKey, $this->article_model->publishedField, $_REQUEST['items'], $_REQUEST['action']);
		echo $this->utils->php2js($data);
	}

	function getCategoryByAlias($category_alias)
	{	
		$id_category = '';
		
		if (!empty($category_alias))
		{
			if ($category_alias == 'static')
			{
				$id_category = 1;	
			}
			else if ($category_alias == 'news')
			{
				$id_category = 2;	
			}	
		}
		
		return $id_category;
	}
	
	/*
	 * Static blog 
	*/
	function view($page_alias, /*$topic,*/ $id_category = PAGE_CATEGORY_STATIC)
	{
		/*
		$this->id_category = $id_category;
		$this->data['Title'] = '';
		
		$this->data['page'] = $this->post_model->getByAlias($page_alias);
		
		if (!empty($this->data['page']))
		{
			$this->data['Title'] = $this->data['page']->title;
		}
		*/
		
		$this->view = '/blog/'.$page_alias;//.'/'.$topic;
		
		$this->renderLayout();	
	}
	
	/*
	 * News blog 
	*/
	function news($page_alias = 'all', $id_category = PAGE_CATEGORY_ADVERTISER)
	{
		$this->id_category = $id_category;
		$this->data['Title'] = '';
		
		if($page_alias == 'all')
		{
			$this->data['news_all'] = $this->post_model->getByAlias();
			$this->data['Title'] = T_('Все новости');
			$this->view = '/blog/news';
		}
		else
		{
			$this->data['page'] = $this->post_model->getByAlias($page_alias);
			
			if (!empty($this->data['page']))
			{
				$this->data['Title'] = $this->data['page']->title;
			}
			
			$this->view = '/blog/view';
		}
		
		$this->renderLayout();	
	}
}
?>